package com.movie.modules.jf.service.impl;

import com.movie.common.utils.NumberUtils;
import com.movie.modules.jf.entity.*;
import com.movie.modules.jf.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.movie.common.utils.PageUtils;
import com.movie.common.utils.Query;

import com.movie.modules.jf.dao.MovieDao;
import org.springframework.transaction.annotation.Transactional;


@Service("movieService")
@Transactional
public class MovieServiceImpl extends ServiceImpl<MovieDao, MovieEntity> implements MovieService {

    @Autowired
    private RelationService relationService;
    @Autowired
    private ImageResourceService imageResourceService;
    @Autowired
    private MovieCommentService commentService;
    @Autowired
    private ActorService actorService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private MovieDao movieDao;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String name=params.get("name")==null?null:params.get("name").toString();
        Integer movieStatus= NumberUtils.objToInteger(params.get("movieStatus"));
        String releaseDate=params.get("releaseDateArr")==null?null:params.get("releaseDateArr").toString();
        String beginDate=StringUtils.isNotEmpty(releaseDate)?releaseDate.split(",")[0]:null;
        String endDate=StringUtils.isNotEmpty(releaseDate)?releaseDate.split(",")[1]:null;
        IPage<MovieEntity> page = this.page(
                new Query<MovieEntity>().getPage(params),
                new QueryWrapper<MovieEntity>().eq(StringUtils.isNotEmpty(name),"name",name).eq(movieStatus!=null,"movie_status",movieStatus).between(StringUtils.isNotEmpty(releaseDate),"release_date",beginDate,endDate).orderByDesc("movie_id")
        );

        return new PageUtils(page);
    }

    @Override
    public void update(MovieEntity movie) {
        if(movie.getMovieId()!=null){
            deleteByMovieId(movie.getMovieId(),false);
        }
        if(movie.getImages()!=null && movie.getImages().length>0){
            movie.setCover(movie.getImages()[0]);
        }else{
            movie.setCover("cover.jpg");
        }
        if(this.saveOrUpdate(movie)){
            if(movie.getActors()!=null){
                /*处理演员和电影的关系*/
                List<RelationEntity> movieActorList = new ArrayList<>();
                for (Integer actorId : movie.getActors()) {
                    RelationEntity relation = new RelationEntity();
                    relation.setResourceId(movie.getMovieId());
                    relation.setRelationId(actorId);
                    relation.setRelationType(2);
                    movieActorList.add(relation);
                }
                /*保存演员和电影关系*/
                relationService.saveBatch(movieActorList);
            }
            if(movie.getTypes()!=null){
                /*处理类型和电影的关系*/
                List<RelationEntity> movieTypeList=new ArrayList<>();
                for(Integer typeId : movie.getTypes()){
                    RelationEntity relation=new RelationEntity();
                    relation.setResourceId(movie.getMovieId());
                    relation.setRelationId(typeId);
                    relation.setRelationType(1);
                    movieTypeList.add(relation);
                }
                /*保存类型和电影的关系*/
                relationService.saveBatch(movieTypeList);
            }
            if(movie.getImages()!=null){
                /*处理图片和电影的关系*/
                List<ImageResourceEntity> imageList=new ArrayList<>();
                for(String img : movie.getImages()){
                    ImageResourceEntity image=new ImageResourceEntity();
                    image.setImageSrc(img);
                    image.setResourceType(1);
                    image.setResourceId(movie.getMovieId());
                    imageList.add(image);
                }
                /*保存图片*/
                imageResourceService.saveBatch(imageList);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        if(this.removeById(id)){
            deleteByMovieId(id,true);
        }
    }

    @Override
    public Map<String, Object> getInfoById(Integer id) {
        Map<String,Object> map=new HashMap<>();
        MovieEntity movie=this.getById(id);
        if(movie!=null){
            MovieEntity movieEntity=movieDao.getMovieCommentScore(id);
            movie.setScore(movieEntity.getScore());
            movie.setCommentCount(movieEntity.getCommentCount());
            map.put("movie",movie);
            map.put("actorList",actorService.getByMovieId(id));
            map.put("typeList",typeService.getByMovieId(id));
            map.put("imageList",imageResourceService.list(new QueryWrapper<ImageResourceEntity>().eq("resource_id",id).eq("resource_type",1).eq("status",1)));
        }
        return map;
    }

    @Override
    public List<MovieEntity> getHotMovieList(int limit) {
        return movieDao.getMovieByComment(limit);
    }

    @Override
    public List<MovieEntity> getMovieVip(Integer movieId, Integer vipId) {
        return movieDao.getMovieByVipAndMovie(movieId, vipId);
    }

    @Override
    public PageUtils getMovieVipList(Map<String, Object> params, Integer vipId) {
        IPage<MovieEntity> page= movieDao.getMovieByVipAndMovieList(new Query<MovieEntity>().getPage(params),vipId);
        return new PageUtils(page);
    }

    @Override
    public PageUtils getMovieByTypeId(Map<String, Object> params, Integer typeId) {
        return new PageUtils(movieDao.getMovieByTypeId(new Query<MovieEntity>().getPage(params),typeId));
    }


    private void deleteByMovieId(int movieId,boolean delComment){
        /*删除所有演员信息*/
        relationService.remove(new QueryWrapper<RelationEntity>().eq("resource_id",movieId).and(qw->qw.eq("relation_type",2)));
        /*删除所有类型信息*/
        relationService.remove(new QueryWrapper<RelationEntity>().eq("resource_id",movieId).and(qw->qw.eq("relation_type",1)));
        /*删除所有图片信息*/
        imageResourceService.remove(new QueryWrapper<ImageResourceEntity>().eq("resource_id",movieId).and(qw->qw.eq("resource_type",1)));
        if(delComment){
            /*删除所有评论信息*/
            commentService.remove(new QueryWrapper<MovieCommentEntity>().eq("movie_id",movieId));
        }
    }
}
